package com.hyperf.core.snowflake;

import com.hyperf.core.snowflake.exception.SnowflakeException;

/**
 * @author lizhaoyang
 */
public class SnowflakeTemplate {

    private WorkerRegister workerRegister;

    public SnowflakeTemplate() {
    }

    public SnowflakeTemplate(WorkerRegister workerRegister) {
        this.workerRegister = workerRegister;
    }

    public void start() {
        if (workerRegister == null) {
            throw new SnowflakeException("workerRegister can not be null, please check your configuration");
        }

        workerRegister.registerWorker();
    }

    public void destroy() {
        if (workerRegister == null) {
            throw new SnowflakeException("workerRegister can not be null, please check your configuration");
        }

        workerRegister.deleteWorker();
    }

    public long nextId() {
        return workerRegister.getSnowflakeUtil().nextId();
    }

    public String nextIdStr() {
        return workerRegister.getSnowflakeUtil().nextIdStr();
    }

    public WorkerRegister getWorkerRegister() {
        return workerRegister;
    }

    public void setWorkerRegister(WorkerRegister workerRegister) {
        this.workerRegister = workerRegister;
    }
}
